<template>
  <!-- 商品分类列表 -->
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="110px">
      <el-form-item label="商品分类名称" style="margin-left: -10px">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入商品分类名称"
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAddOrder"
        >新增</el-button
      >
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
        >刷新</el-button
      >
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        prop=""
        width="150px"
      />
      <el-table-column
        label="商品分类名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="className"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        :show-overflow-tooltip="true"
        prop="remarks"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        :show-overflow-tooltip="true"
        prop=""
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleGoEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新建商品 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @closed = "cancel">
      <div class="xian_"></div>
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rulesForm"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="商品分类名称"
              prop="className"
              label-width="120px"
            >
              <el-input
              style="width:300px"
                v-model="ruleForm.className"
                placeholder="请输入商品分类名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="创建人" label-width="100px" style=" Text-align:justify;

text-align-last:justify;padding-left:20px">
              <div style="letter-spacing:1px">{{operator}}</div>
              <!-- <el-input
                v-model="ruleForm.remarks"
                type="textarea"
                placeholder="请输入内容"
              ></el-input> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('form')"
          v-if="title == '新增商品分类'"
          >确 定</el-button >
        <el-button
          type="primary"
          @click="submitFormEdit('form')"
          v-if="title == '编辑商品分类'"
          >确 定</el-button >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { goodsClassList, goodsClassAdd ,goodsClassEdit} from "@/api/goods/index";
export default {
  data() {
    return {
      operator : "",//创建人
      title: "",
      ruleForm: {
        // 遮罩层
        className: null,
        remarks: null,
      },
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classId: null,
        className: null,
      },
      rulesForm: {
        className: [
          { message: "请输入商品分类名称", trigger: "blur", required: true },
        ],
      },
    };
  },
  created() {
    this.getList();
     this.operator = localStorage.getItem("userName");
  },
  methods: {
    handleGoEdit(row) {
      //编辑分类
      this.title = "编辑商品分类";
      this.open = true
      this.ruleForm.className = row.className
      this.ruleForm.remarks = row.remarks
      this.ruleForm.classId = row.classId
    },
    submitFormEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsClassEdit(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "编辑商品分类成功",
                type: "success",
              });
              this.open = false;
              setTimeout(() => {
                this.getList();
                this.ruleForm = {
                  className: "",
                  remarks: "",
                  classId : ""
                };
              }, 100);
            }
          });
        } else {
          return false;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsClassAdd(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "新增商品分类成功",
                type: "success",
              });
              this.open = false;
              setTimeout(() => {
                this.getList();
                this.ruleForm = {
                  className: "",
                  remarks: "",
                };
              }, 100);
            }
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      //创建取消
      this.open = false;
      this.ruleForm.className = null;
      this.ruleForm.remarks = null;
      this.ruleForm.classId = null
    },
    handleAddOrder() {
      this.title = "新增商品分类";
      this.open = true;
    },
    handleClearCache() {
      window.location.reload();
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      goodsClassList(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      //重置事件
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        className: null,
        classId: null,
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.xian_ {
  height: 1px;
  width: 500px;
  background-color: #eee;
  margin-left: -20px;
  margin-top: -25px;
  margin-bottom: 30px;
}
  .dialog-footer {
   margin-top: -10px;
    display: flex;
    justify-content:  center;
  }
.el-input--medium{
   margin-bottom: 10px  !important;
}
.el-range-editor {
  width: 100% !important;
}

.app-container {

  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
    .create {
      margin-right: 20 px;
      float: right;
    }
  }
}
</style>